package com.video.commons.runtimelog;

/**
 * Threadlocal用来保存请求的id标识，日志记录时方便通过请求id进行追踪
 */
public class RequestIdThreadLocal {

    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();
    private static ThreadLocal<Integer> number = new ThreadLocal<>();
    private static ThreadLocal<Boolean> isLog = new ThreadLocal<>();

    public static void putLog(boolean flag){
        isLog.set(flag);
    }

    public static boolean isLog(){
        return isLog.get() != null ? isLog.get() : false;
    }

    public static void putId(String uuid){
        threadLocal.set(uuid);
    }

    public static String getId(){
        return threadLocal.get();
    }

    public static Integer getNumber(){
        Integer n = number.get();
        if (n == null) number.set(n = 0);
        number.set(++n);
        return n;
    }

    public static void clear(){
        threadLocal.remove();
        number.remove();
        isLog.remove();
    }
}
